package o;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.ImageHeaderParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

class sz {
    private static final sy ˏ = new sy();
    private final List<ImageHeaderParser> ʻ;
    private final sw ˊ;
    private final ud ˋ;
    private final sy ˎ;
    private final ContentResolver ॱ;

    sz(List<ImageHeaderParser> list, sw swVar, ud udVar, ContentResolver contentResolver) {
        this(list, ˏ, swVar, udVar, contentResolver);
    }

    sz(List<ImageHeaderParser> list, sy syVar, sw swVar, ud udVar, ContentResolver contentResolver) {
        this.ˎ = syVar;
        this.ˊ = swVar;
        this.ˋ = udVar;
        this.ॱ = contentResolver;
        this.ʻ = list;
    }

    int ॱ(Uri uri) {
        Throwable e;
        InputStream inputStream = null;
        try {
            inputStream = this.ॱ.openInputStream(uri);
            int ˎ = rx.ˎ(this.ʻ, inputStream, this.ˋ);
            if (inputStream == null) {
                return ˎ;
            }
            try {
                inputStream.close();
                return ˎ;
            } catch (IOException e2) {
                return ˎ;
            }
        } catch (IOException e3) {
            e = e3;
            try {
                if (Log.isLoggable("ThumbStreamOpener", 3)) {
                    Log.d("ThumbStreamOpener", "Failed to open uri: " + uri, e);
                }
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e4) {
                    }
                }
                return -1;
            } catch (Throwable th) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e5) {
                    }
                }
            }
        } catch (NullPointerException e6) {
            e = e6;
            if (Log.isLoggable("ThumbStreamOpener", 3)) {
                Log.d("ThumbStreamOpener", "Failed to open uri: " + uri, e);
            }
            if (inputStream != null) {
                inputStream.close();
            }
            return -1;
        }
    }

    public InputStream ˊ(Uri uri) throws FileNotFoundException {
        InputStream inputStream = null;
        String ˋ = ˋ(uri);
        if (!TextUtils.isEmpty(ˋ)) {
            File ॱ = this.ˎ.ॱ(ˋ);
            if (ˊ(ॱ)) {
                Uri fromFile = Uri.fromFile(ॱ);
                try {
                    inputStream = this.ॱ.openInputStream(fromFile);
                } catch (Throwable e) {
                    throw ((FileNotFoundException) new FileNotFoundException("NPE opening uri: " + uri + " -> " + fromFile).initCause(e));
                }
            }
        }
        return inputStream;
    }

    @Nullable
    private String ˋ(@NonNull Uri uri) {
        Cursor ˋ;
        Throwable e;
        Throwable th;
        String str = null;
        try {
            ˋ = this.ˊ.ˋ(uri);
            if (ˋ != null) {
                try {
                    if (ˋ.moveToFirst()) {
                        str = ˋ.getString(0);
                        if (ˋ != null) {
                            ˋ.close();
                        }
                        return str;
                    }
                } catch (SecurityException e2) {
                    e = e2;
                    try {
                        if (Log.isLoggable("ThumbStreamOpener", 3)) {
                            Log.d("ThumbStreamOpener", "Failed to query for thumbnail for Uri: " + uri, e);
                        }
                        if (ˋ != null) {
                            ˋ.close();
                        }
                        return str;
                    } catch (Throwable th2) {
                        th = th2;
                        if (ˋ != null) {
                            ˋ.close();
                        }
                        throw th;
                    }
                }
            }
            if (ˋ != null) {
                ˋ.close();
            }
        } catch (SecurityException e3) {
            e = e3;
            ˋ = null;
            if (Log.isLoggable("ThumbStreamOpener", 3)) {
                Log.d("ThumbStreamOpener", "Failed to query for thumbnail for Uri: " + uri, e);
            }
            if (ˋ != null) {
                ˋ.close();
            }
            return str;
        } catch (Throwable e4) {
            ˋ = null;
            th = e4;
            if (ˋ != null) {
                ˋ.close();
            }
            throw th;
        }
        return str;
    }

    private boolean ˊ(File file) {
        return this.ˎ.ॱ(file) && 0 < this.ˎ.ˋ(file);
    }
}
